package com.mypro._01base.test;

import com.mypro._01base.dbutil.DbUtil;
import com.mypro._01base.entity.Dept;
import com.mypro._01base.entity.Emp;
import com.mypro._01base.mapper.DeptMapper;
import com.mypro._01base.mapper.EmpMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * @author user_xdd
 * @Date 2022-05-14 15:34
 * @description  03  测试员工和缓存
 *    1. MyBatis执行session时 会在内存中划出一个缓存区间，已经有的对象 再次访问不会执行SQL操作 直接从缓存里去
 *         如果中间执行过任何 增删改的操作  则会再次查询
 *    2. session.clearCache();//清空缓存区数据
 *    3. session一旦关闭 缓存区间也就消失了，重新创建会划出新的缓存区间
 *
 * @return
 */
public class emp_cache {
    private static final Logger LOGGER = Logger.getLogger(emp_cache.class);

    @Test
    public void emp() {
        SqlSession session = DbUtil.getSqlSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);

        Emp emp1 =  empMapper.queryByPrimaryKey(6689);
         LOGGER.debug("    emp1:"+emp1);
        Dept dept = emp1.getDept();
         LOGGER.debug("    dept:"+dept);

        //empMapper.delete(9999);
        session.clearCache();//清空缓存区数据

        Emp emp2 = empMapper.queryByPrimaryKey(6689);

        session.commit();
        session.close();

        //session一旦关闭 缓存区间也就消失了，重新创建会划出新的缓存区间
        SqlSession session2 = DbUtil.getSqlSession();
        EmpMapper empMapper2 = session2.getMapper(EmpMapper.class);
        Emp emp3 = empMapper2.queryByPrimaryKey(6689);
        System.out.println(emp3);
        

         LOGGER.debug("==============测试一对多========================");
        DeptMapper deptMapper = session2.getMapper(DeptMapper.class);
        List<Dept> depts = deptMapper.queryAll();
        for (Dept dept1 : depts) {
             LOGGER.debug("    dept1:"+dept1);
            List<Emp> empList = dept1.getEmpList();
            for (Emp emp : empList) {
                 LOGGER.debug("    emp:"+emp);
                
            }
        }
        
        
        session2.close();

    }
}
